<?php
/**
 * 飞信机器人--功能插件--天气预报
 * 功能：
 * 数据表：
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
支持参数(fx_menus表 extra_params字段)：
city=城市名（utf8格式）
*/
class Weather extends CommonPlugin
{
	var $UUID='fb939d60-07f9-11de-8c30-0800200c9a66';

	public function Weather(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	function getTips()
	{
		return "请输入要查询的城市。".$this->getFooter();
	}
	
	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
		
		$str = iconv('utf-8','gbk', $input);
		$url = "http://www.google.cn/search?as_q=tq+{city}&num=1";
		
		$url = str_replace('{city}', urlencode($input), $url);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		
		ob_start(); 
		curl_exec($ch);
		$content = ob_get_contents();
		ob_end_clean();
		
		curl_close($ch);
	
		$start = "<div style=\"padding:5px;float:left\">";
		$end  = "</div>";

		$posStart = strpos($content, $start)+strlen($start);
		$posEnd = strpos($content, $end, $posStart+1);
		$posEnd = strpos($content, $end, $posEnd+1);
		$posEnd = strpos($content, $end, $posEnd+1);
		$posEnd = strpos($content, $end, $posEnd+1);
		$posEnd = strpos($content, $end, $posEnd+1);
		$posEnd = strpos($content, $end, $posEnd+1);
		
		$weatherStr = substr($content, $posStart, $posEnd-$posStart);
		$weatherStr = strip_tags($weatherStr);

		$content = iconv('gbk','utf-8', $weatherStr);
				
		$content = mb_ereg_replace('当前',' 当前', $content);
		$content = mb_ereg_replace('温度',' 温度 ', $content);
		$content = mb_ereg_replace('今日',' 今日', $content);
		$content = mb_ereg_replace('周',' 周', $content);
	
		if( strlen( $content ) > 500 )
			$content = "无该城市天气信息";

		return $content.$this->getFooter();	
	}	
}

?>
